package com.aaa.system.service.impl;

import com.aaa.entity.DictData;
import com.aaa.system.dao.DictDataDao;
import com.aaa.system.service.DictDataService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: aaa-his-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-12-08 12:00
 **/
@Service
@Slf4j
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataDao dictDataDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public List<DictData> findByType(String dictType) {
        try{
            //1.从缓存中查询数据字段的信息
            String s = redisTemplate.opsForValue().get("dict:" + dictType);
            if(StringUtils.hasText(s)){
                List<DictData> dictData = JSON.parseArray(s, DictData.class);
                return dictData;
            }
        }catch (Exception e){
            log.error("redis服务故障");
        }
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type",dictType);
        List<DictData> dictData = dictDataDao.selectList(wrapper);
        redisTemplate.opsForValue().set("dict:"+dictType,JSON.toJSONString(dictData));
        return dictData;
    }
}
